//
//  RecordViewController.m
//  TourPlay
//
//  Created by dllo on 15/12/2.
//  Copyright © 2015年 王军岐. All rights reserved.
//

#import "RecordViewController.h"
#import "ActivitieDetailsViewController.h"


@interface RecordViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *myTableV;
@property (nonatomic, retain) NSMutableArray *myArray;

@end

@implementation RecordViewController
- (void)dealloc
{
    [_myTableV release];
    [_myArray release];
    [super dealloc];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self dateUp];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.myArray = [NSMutableArray array];
    
    self.navigationItem.title = @"浏览历史";
    
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(0, 0, 60, 44);
//    rightButton.backgroundColor = [UIColor orangeColor];
    [rightButton setTitle:@"开启无痕" forState:UIControlStateNormal];
    [rightButton setTitle:@"正在无痕" forState:UIControlStateSelected];
    
    [rightButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:15.f];
    [rightButton setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor brownColor] forState:UIControlStateSelected];
    [rightButton addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"isRecord"] isEqualToString:@"1"]) {
        
        rightButton.selected = NO;
    } else {
        rightButton.selected = YES;
    }
    
    
    
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:rightButton] autorelease];
    
    
//    self.myArray = [[Singleton shareSingleton] selectRecordActivityWithSQL:@"select * from Record"];
    
    
    
}
- (void)dateUp
{
    NSMutableArray *array = [[Singleton shareSingleton] selectRecordActivityWithSQL:@"select * from Record"];
    [self.myArray removeAllObjects];
    
    for (NSInteger i = array.count - 1; i >= 0; i--) {
        [self.myArray addObject:[array objectAtIndex:i]];
    }
    
    [self.myTableV reloadData];
    
}
- (UITableView *)myTableV
{
    if (nil == _myTableV) {
        self.myTableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
        self.myTableV.delegate = self;
        self.myTableV.dataSource = self;
        self.myTableV.rowHeight = 50.f;
        self.myTableV.tableFooterView = [UIView new];
        self.myTableV.backgroundColor = [UIColor clearColor];
        [self.view addSubview:self.myTableV];
        [_myTableV release];
    }
    return _myTableV;
}
- (void)rightAction:(UIButton *)sender
{
    if (sender.selected) {
//        NSLog(@"没有开启");
        // 没有开启无痕就记录
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"isRecord"];
    } else {
//        NSLog(@"已开启无痕");
        [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"isRecord"];
    }
    sender.selected = !sender.selected;
    

}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecordAcitvityMod *mod = [self.myArray objectAtIndex:indexPath.row];
    
    [[Singleton shareSingleton] exeWithSQL:[NSString stringWithFormat:@"DELETE FROM Record WHERE Id = %@", mod.Id]];
    [self.myArray removeObjectAtIndex:indexPath.row];
    [self.myTableV deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.myArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (nil == cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"] autorelease];
    }
    
    RecordAcitvityMod *mod = [self.myArray objectAtIndex:indexPath.row];
    cell.textLabel.text = mod.title;
    cell.detailTextLabel.text = mod.time;
    
    // 右箭头
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    
    [self addColorChangedBlock:^{
        
        cell.nightBackgroundColor = [ColorSingleton shareSingleton].cellColor;
        cell.normalBackgroundColor = [ColorSingleton shareSingleton].cellColor;
        
        cell.contentView.nightBackgroundColor = [ColorSingleton shareSingleton].cellColor;
        cell.contentView.normalBackgroundColor = [ColorSingleton shareSingleton].cellColor;
        
        cell.textLabel.nightTextColor = [ColorSingleton shareSingleton].cellTitleColor;
        cell.textLabel.normalTextColor = [ColorSingleton shareSingleton].cellTitleColor;
        
        cell.detailTextLabel.textColor = [ColorSingleton shareSingleton].cellTitleColor;
        cell.detailTextLabel.textColor = [ColorSingleton shareSingleton].cellTitleColor;
    }];
    
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    ActivitieDetailsViewController *activitieVC = [[ActivitieDetailsViewController alloc] init];
    
    RecordAcitvityMod *mod = [self.myArray objectAtIndex:indexPath.row];
    activitieVC.activityId = mod.Id;
    activitieVC.activityTitle = mod.title;
    // 隐藏
    activitieVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:activitieVC animated:YES];
    [activitieVC release];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
